package com.hz.foodstalls.job;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.hz.foodstalls.model.Reserve;
import com.hz.foodstalls.model.Restaurant;
import com.hz.foodstalls.model.SmsReturn;
import com.hz.foodstalls.model.SmsSend;
import com.hz.foodstalls.util.SmsUtil;
import com.hz.yunpian.model.SendBatchInfo;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;

public class ReserveJob implements Job {

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        Db.tx(new IAtom() {// 事务控制
            // 在执行run的过程中，如果有异常将回滚，或者return false 将回滚
            @Override
            public boolean run() throws SQLException {
                // 处理抢单信息
                Date date = new Date();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                List<Reserve> list = Reserve.dao.findState1();// state = 1 还未被抢单
                if (list != null && list.size() > 0) {
                    for (Reserve reserve : list) {
                        List<SmsReturn> smslist = SmsReturn.dao.findByReserveId(reserve.getReserveId());
                        if (smslist != null && smslist.size() > 0) {
                            SmsReturn smsreturn = smslist.get(0);
                            String text = "【沈家门海鲜夜排档】抢单成功!共" + reserve.getPeoperNum() + "人,时间:"
                                    + reserve.getReserveDate() + ",联系人:" + reserve.getName() + ",电话:"
                                    + reserve.getMobile();
                            /*
                             * if (reserve.getNeedroom() == 1) { text =
                             * "恭喜你，抢单成功！订单号:" + reserve.getReserveId() + "，人数:"
                             * + reserve.getPeoperNum() + "，需要包间，时间:" +
                             * reserve.getReserveDate() + "，客户联系信息为:" +
                             * reserve.getName() + "--" + reserve.getMobile(); }
                             * if (reserve.getNeedroom() == 0) { text =
                             * "恭喜你，抢单成功！订单号:" + reserve.getReserveId() + "，人数:"
                             * + reserve.getPeoperNum() + "，不需要包间，时间:" +
                             * reserve.getReserveDate() + "，客户联系信息为:" +
                             * reserve.getName() + "--" + reserve.getMobile(); }
                             */
                            Restaurant restaurant = Restaurant.dao.findByTel(smsreturn.getMobile());
                            if (null == restaurant)
                                continue;

                            SendBatchInfo batchinfo = SmsUtil.smsSend(smsreturn.getMobile(), text);// 发送短信通知商家
                            int total_count = batchinfo.getTotal_count();
                            // 将短信发送记录存入数据库
                            SmsSend smssend = new SmsSend();
                            smssend.setReserveId(smsreturn.getReserveId());
                            smssend.setText(text);
                            smssend.setType("2");
                            smssend.setMobiles(smsreturn.getMobile());
                            smssend.setSmsNum(total_count);
                            smssend.setSendTime(df.format(date));
                            smssend.save();

                            // 发送短信通知客户
                            String text2 = "【沈家门海鲜夜排档】订位成功!摊主电话:" + restaurant.getPhone() + ",用餐高峰期,请准时到达夜排档"
                                    + restaurant.getSerialNumber() + "摊位,否则会存在等位现象";
                            SendBatchInfo batchinfo2 = SmsUtil.smsSend(reserve.getMobile(), text2);
                            int total_count2 = batchinfo2.getTotal_count();
                            // 将短信发送记录存入数据库
                            SmsSend smssend2 = new SmsSend();
                            smssend2.setReserveId(smsreturn.getReserveId());
                            smssend2.setText(text2);
                            smssend2.setType("3");
                            smssend2.setMobiles(reserve.getMobile());
                            smssend2.setSmsNum(total_count2);
                            smssend2.setSendTime(df.format(date));
                            smssend2.save();

                            // 订单状态改为2
                            reserve.setReservestate(2);
                            reserve.setStoreId(restaurant.getId());
                            reserve.update();
                        }
                    }
                }

                // 处理过期订单
                List<Reserve> reservelist = Reserve.dao.findfailure(df.format(date));
                for (Reserve r : reservelist) {
                    r.setReservestate(-1);
                    r.update();
                }
                return true;
            }
        });
    }

}
